package com.woniuxy.mq02.configuration;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author author
 * @Create 2024-02-29 18:57
 */

@Configuration
public class RabbitConfiguration {

    //org.springframework.http.converter.HttpMessageConverter,
    //Servlet  AJAX发送异步请求----->对象调用方法转JSON------>响应前端
    // SpringMVC底层提供的消息转换器：@ResponseBody：自动将对象转JSON（底层使用HttpMessageConverter）
    //RabbitMQ底层将对象转成JDK二进制格式，底层使用了默认的消息转换器
    //自定义序列化格式
    @Bean
    public MessageConverter messageConverter(){
        return  new Jackson2JsonMessageConverter();
    }

    //定义队列 channel.queueDeclare
    @Bean
    public Queue helloworld(){
        return new Queue("helloworld");
    }
    //定义邮件队列<bean id="方法名" class="" />
    @Bean
    public Queue queueemail(){
        return new Queue("queueemail");
    }
    //定义短信队列
    @Bean
    public Queue queuesms(){
        return new Queue("queuesms");
    }

    //使用发布订阅模式(fanout扇出)模式，使用Fanout类型的交换机
    @Bean
    public FanoutExchange exchangefanout(){
        return new FanoutExchange("exchangefanout");
    }

    //将队列绑定到交换机,fantout模式，绑定邮件队列
    @Bean
    public Binding bindingqueueemailtoexchangefanout(Queue queueemail,FanoutExchange exchangefanout){
//        return BindingBuilder.bind(queueemail()).to(exchangefanout());
        return BindingBuilder.bind(queueemail).to(exchangefanout);
    }
    //将队列绑定到交换机,fantout模式，绑定短信队列
    @Bean
    public Binding bindingqueuesmstoexchangefanout(Queue queuesms,FanoutExchange exchangefanout){
//        return BindingBuilder.bind(queueemail()).to(exchangefanout());
        return BindingBuilder.bind(queuesms).to(exchangefanout);
    }

    //使用路由模式,使用Direct类型的交换机
    @Bean
    public DirectExchange exchangedirect(){
        return new DirectExchange("exchangedirect");
    }
    //将队列绑定到交换机,routing模式，绑定邮件队列，绑定是要指定明确的路由名
    @Bean
    public Binding bindingqueueemailtoexchangedirect(Queue queueemail,DirectExchange exchangedirect){
        return BindingBuilder.bind(queueemail).to(exchangedirect).with("email");
    }
    //将队列绑定到交换机,routing模式，绑定短信队列，绑定是要指定明确的路由名
    @Bean
    public Binding bindingqueuesmstoexchangedirect(Queue queuesms,DirectExchange exchangedirect){
        return BindingBuilder.bind(queuesms).to(exchangedirect).with("sms");
    }

    //使用主题模式,使用Topic类型的交换机
    @Bean
    public TopicExchange exchangetopic(){
        return new TopicExchange("exchangetopic");
    }
    //将队列绑定到交换机,主题模式，绑定邮件队列，绑定是要指定支持通配符的路由名
    @Bean
    public Binding bindingqueueemailtoexchangetopic(Queue queueemail,TopicExchange exchangetopic){
        return BindingBuilder.bind(queueemail).to(exchangetopic).with("#.email.#");
    }
    //将队列绑定到交换机,主题模式，绑定短信队列，绑定是要指定支持通配符的路由名
    @Bean
    public Binding bindingqueuesmstoexchangetopic(Queue queuesms,TopicExchange exchangetopic){
        return BindingBuilder.bind(queuesms).to(exchangetopic).with("#.sms.#");
    }
}
